#include <iostream>
#include <vector>
#include <climits>
using namespace std;

//其实就是找连续和最小的连续 数组长-k 个元素
//注意要处理下最后一个子数组处理不要越界
class Solution {
public:
    int maxScore(vector<int>& cardPoints, int k) {
	long long int sum = 0;
	for(int i = 0; i < cardPoints.size(); ++i)
	{
		sum += cardPoints[i];
	}

	int len = cardPoints.size() - k;
	
	if(len == 0)
	{
		return sum;
	}

	int begin = 0;
	int end = 0;
	long long int temp = cardPoints[0];
	long long int res = LONG_LONG_MAX;
	while((begin < cardPoints.size()) && (end < cardPoints.size()))
	{
		if(end - begin + 1 < len)
		{
			++end;
			temp += cardPoints[end];
		}
		else 
		{
			if(end - begin + 1 == len)
			{
				if(temp < res)
				{
					res = temp;
				}
                		if(end == cardPoints.size() - 1)
                		{
                    			break;
                		}
			}
			temp-=cardPoints[begin];
			++begin;
		}
	}
	return sum-res;
    }
};

int main()
{
	vector<int> a = {1,79,80,1,1,1,200,1};
	Solution b;
	cout<<b.maxScore(a, 3)<<endl;
}
